package com.heima.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.mapper.RoleMapper;
import com.heima.pojo.User;
import com.heima.pojo.domain.PageQuery;
import com.heima.pojo.domain.PageResult;
import com.heima.pojo.domain.Role;
import com.heima.pojo.dto.UserDto;
import com.heima.service.UserService;
import com.heima.utils.SecretKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-service/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 人员搜索
     * @param pageQuery
     * @param userName
     * @return
     */

    @GetMapping("/search")
    public PageResult search(@ModelAttribute PageQuery pageQuery, @RequestParam(required = false) String userName){

       return userService.search(pageQuery,userName);
    }

    /**
     * 新增人员
     */
/*    @GetMapping
    public List<Role> searRole(){
        return roleMapper.selectList(null);
    }*/
    @PostMapping
    public Boolean addUser(@RequestBody UserDto userDto){
        return userService.insert(userDto);
    }

    /**
     * 修改人员
     */
    @PutMapping("/{userId}")
    public Boolean update(@RequestBody UserDto userDto, @PathVariable Integer userId){
        return userService.updateUser(userDto,userId);
    }
    /**
     * 删除人员
     */
    @DeleteMapping("/{userId}")
    public Boolean del(@PathVariable Integer userId){
        return userService.remove(Wrappers.<User>lambdaQuery().eq(User::getId,userId));
    }
}
